const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const webpack = require('webpack');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCssAssetsPlugin = require("optimize-css-assets-webpack-plugin");


module.exports = {
    entry: './src/main.js',
    // entry: ['./src/js01.js','./src/js02.js'],
    // entry: {'js01': './src/js01.js', 'js02': './src/js02.js'},
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'bundle.js',
        // filename: '[name].bundle.js'
    },
    // devtool: 'none',
    devtool: 'cheap-module-eval-source-map',
    plugins: [
        new HtmlWebpackPlugin(),
        new CleanWebpackPlugin(['./dist']),
        new webpack.HotModuleReplacementPlugin(),
        new MiniCssExtractPlugin({filename: 'css/index.css'}),
        new OptimizeCssAssetsPlugin(),
        new webpack.ProvidePlugin({
            $: 'jquery',
        })
    ],
    devServer: {
        host: 'localhost',
        port: 9090,
        open: true,
        hot: true
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: "babel-loader",
                options: {
                    "presets": ["@babel/preset-env"]
                }
            },
            {
                test: /\.css$/,
                // use: [MiniCssExtractPlugin.loader, 'css-loader']
                use: [
                    {
                        loader: MiniCssExtractPlugin.loader,
                        options: {
                            publicPath: '../'
                        }
                    }, 'css-loader']
            },
            {
                test: /\.(jpg|png|gif)$/,
                // use: ['file-loader']
                use: [{
                    loader: 'url-loader',
                    options: {
                        limit: 50 * 1024,
                        outputPath: 'images'
                    }
                }]
            }
        ]
    },
    optimization: {
        splitChunks: {
            cacheGroups: {
                commonjs: {
                    test: /\.js$/,
                    chunks: 'initial'
                }
            }
        }
    }
};
